package com.mac.bluetoothbt.feature;

import java.io.Serializable;

import static com.mac.bluetoothbt.feature.DeviceEntity.DISPLAYVAL_OFFSET;
import static com.mac.bluetoothbt.feature.DeviceEntity.FACTORYCHAR_OFFSET;
import static com.mac.bluetoothbt.feature.DeviceEntity.GASINDEX_OFFSET;
import static com.mac.bluetoothbt.feature.DeviceEntity.MEASURERANGEINDEX_OFFSET;
import static com.mac.bluetoothbt.feature.DeviceEntity.PRESSUREMAX_OFFSET;
import static com.mac.bluetoothbt.feature.DeviceEntity.PRESSUREMIN_OFFSET;
import static com.mac.bluetoothbt.feature.DeviceEntity.SENSOR_LENGTH;
import static com.mac.bluetoothbt.feature.DeviceEntity.STATE_OFFSET;

/**
 * 传感器类
 */
class Sensor  implements Serializable {
    /**
     * 出厂字节
     */
    private int factoryChar = 0;
    /**
     * 气体索引
     */
    private int gasIndex = 0;
    /**
     * 压力上限
     */
    private float pressureMax = 0;
    /**
     * 压力下限
     */
    private float pressureMin = 0;
    /**
     * 显示值
     */
    private float displayVal = 0;
    /**
     * 状态
     */
    private int state = 0;
    /**
     * 测量范围索引
     */
    private int measureRangeIndex = 0;



    public int getFactoryChar() {
        return factoryChar;
    }

    public void setFactoryChar(int factoryChar) {
        this.factoryChar = factoryChar;
    }

    int getGasIndex(){
        return this.gasIndex;
    }

    public void setGasIndex(int gasIndex) {
        this.gasIndex = gasIndex;
    }

    public float getPressureMax() {
        return pressureMax;
    }

    public void setPressureMax(float pressureMax) {
        this.pressureMax = pressureMax;
    }

    public float getPressureMin() {
        return pressureMin;
    }

    public void setPressureMin(float pressureMin) {
        this.pressureMin = pressureMin;
    }

    public float getDisplayVal() {
        return displayVal;
    }

    public void setDisplayVal(float displayVal) {
        this.displayVal = displayVal;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public int getMeasureRangeIndex() {
        return measureRangeIndex;
    }

    public void setMeasureRangeIndex(int measureRangeIndex) {
        this.measureRangeIndex = measureRangeIndex;
    }




    Sensor(){}

    /**
     * @param bytes 寄存器数据
     * @param offset 偏移量
     */
    Sensor(byte[] bytes, int offset){
        updateSensor(bytes, offset);
    }

    String getGasClassString()
    {
        return FrameUtil.getGas(this.gasIndex);
    }




    String getStateString()
    {
        return FrameUtil.getState(this.state);
    }


    private void updateSensor(byte[] bytes, int index)
    {
        byte[] sub = new byte[SENSOR_LENGTH];
        System.arraycopy(bytes, index, sub, 0, SENSOR_LENGTH);
        updateSensor(sub);
    }

    /**
     * 用获得的数据更新传感器
     * 2byte一寄存器,共10寄存器(出厂字节,气体索引,压力上限,压力上限,压力下限,压力下限,显示值,显示值,状态,测量范围索引),压力上限&下限&显示值&状态 32位float 其他16位整型
     * @param bytes 要写入的参数
     */
    private void updateSensor(byte[] bytes)
    {
        this.factoryChar = FrameUtil.bytes2int16(bytes, FACTORYCHAR_OFFSET);
        this.gasIndex = FrameUtil.bytes2int16(bytes, GASINDEX_OFFSET);

        this.pressureMax = FrameUtil.bytes2float(FrameUtil.getdatafromhex(bytes, PRESSUREMAX_OFFSET));
        this.pressureMin = FrameUtil.bytes2float(FrameUtil.getdatafromhex(bytes, PRESSUREMIN_OFFSET));
        this.displayVal  = FrameUtil.bytes2float(FrameUtil.getdatafromhex(bytes, DISPLAYVAL_OFFSET));

        this.state = FrameUtil.bytes2int16(bytes, STATE_OFFSET);
        this.measureRangeIndex = FrameUtil.bytes2int16(bytes, MEASURERANGEINDEX_OFFSET);
    }
}
